<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/homeIndex' }">
        <i class="el-icon-s-custom"></i> 人事管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>员工离职登记</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-row>
      <el-col :span="24" class="col-in-page">
        <!-- 放搜索条件 
        :inline="ture"让表单元素占一行,一行不够会换行-->
        <el-form :inline="true">
          <el-form-item label="员工姓名">
            <el-input v-model="orderParam.name" placeholder="员工姓名"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="orderParam.tel" placeholder="电话号码"></el-input>
          </el-form-item>
          <el-form-item label="登记时间">
            <el-date-picker v-model="orderParam.leaveTime" placeholder="登记时间"></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="queryEmployee">查询</el-button>
            <el-button type="primary" icon="el-icon-refresh" @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :span="24" class="col-in-page">
        <!-- 放表格数据 -->
        <el-button type="danger" icon="el-icon-plus" @click="putawayVisible=true">新建登记</el-button>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="workNum" label="员工工号" width="180"></el-table-column>
          <el-table-column prop="name" label="员工姓名"></el-table-column>
          <el-table-column prop="gender" label="性别">
            <template slot-scope="scope">{{scope.row.gender == 0?"女":"男"}}</template>
          </el-table-column>
          <el-table-column prop="postName" label="职位"></el-table-column>
          <el-table-column prop="departName" label="部门"></el-table-column>
          <el-table-column prop="tel" label="电话"></el-table-column>
          <el-table-column prop="leaveTime" label="离开时间"></el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" @click="openLookDialog(scope.row.id)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 放分页部分 -->
        <el-pagination
          background
          :page-sizes="[3, 5, 10, 20]"
          :page-size="pageSize"
          :current-page="pageNum"
          layout="total, sizes, prev, pager, next"
          @next-click="nextPage"
          @prev-click="prevPage"
          @size-change="sizeChange"
          @current-change="currentPage"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>

    <!-- 新增登记的弹框 label-width:是label文字占的宽度-->
    <el-dialog title="离职登记" :visible.sync="putawayVisible">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" label-width="100px" required="">
              <el-select v-model="employee.name" placeholder="请选择" @change="handleEmployeeSelect">
                <el-option
                  v-for="idem in employeeList"
                  :key="idem.name"
                  :label="idem.name"
                  :value="idem.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工工号" label-width="100px">
              <el-input v-model="employee.workNum" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工性别" label-width="100px">
              <el-input :value="genderText" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工职位" label-width="100px">
              <el-input v-model="employee.postName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工部门" label-width="100px">
              <el-input v-model="employee.departName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" label-width="100px">
              <el-input v-model="employee.tel" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="离职类型" label-width="100px" required="">
              <el-select placeholder="请选择类型" v-model="employee.leaveType">
                <el-option label="自动离职" value="0"></el-option>
                <el-option label="解雇" value="1"></el-option>
                <el-option label="辞职" value="2"></el-option>
                <el-option label="其他" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职日期" label-width="100px" required="">
              <el-date-picker v-model="employee.leaveTime" type="datetime" placeholder="离职日期"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="离职原因" label-width="100px">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10}"
            placeholder="添加备注..."
            v-model="employee.leaveReason"
          ></el-input>
        </el-form-item>
        <p>当前操作人 : {{ operator.name }}</p>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="putawayVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEmployeeLeave">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查看的弹框 label-width:是label文字占的宽度-->
    <el-dialog title="离职登记" :visible.sync="lookVisible">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" label-width="100px">
              <el-input :value="employee1.name" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工工号" label-width="100px">
              <el-input :value="employee1.workNum" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工性别" label-width="100px">
              <el-input :value="genderText1" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工职位" label-width="100px">
              <el-input :value="employee1.postName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工部门" label-width="100px">
              <el-input :value="employee1.departName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" label-width="100px">
              <el-input :value="employee1.tel" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="离职类型" label-width="100px">
              <el-input :value="leaveTypeText1" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="离职日期" label-width="100px">
              <el-input :value="employee1.leaveTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="离职原因" label-width="100px">
          <el-input :value="employee1.leaveReason" disabled></el-input>
        </el-form-item>
        <p>登记人 : {{ leaveOperatorName }}</p>
      </el-form>
    </el-dialog>
  </div>
</template>
    
<script>
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      total: 0,
      putawayVisible: false,
      lookVisible: false,
      orderParam: {
        name: "",
        tel: "",
        leaveTime: ""
      },
      tableData: [],
      employeeList: [],
      employee: {},
      employee1: {},
      operator: {}, //当前操作人,
      leaveOperatorName: {} //登记人
    };
  },
  methods: {
    clear() {
      this.orderParam.leaveTime = "";
      this.orderParam.name = "";
      this.orderParam.tel = "";
    },
    prevPage(val) {
      //点击上一页触发
      this.pageNum = val;
      this.queryEmployee();
    },
    nextPage(val) {
      //点击下一页触发
      this.pageNum = val;
      this.queryEmployee();
    },
    sizeChange(val) {
      //val每一页条数
      this.pageNum = 1;
      this.pageSize = val;
      this.queryEmployee();
    },
    currentPage(val) {
      this.pageNum = val;
      this.queryEmployee();
    },
    queryCondition() {
      thispageNum = 1;
      this.queryEmployee();
    },
    queryEmployee() {
      //带分页参数pageNum,pageSize
      let pageParam = {};
      pageParam.pageNum = this.pageNum;
      pageParam.pageSize = this.pageSize;
      pageParam.param = this.orderParam;
      if (this.orderParam.leaveTime != "") {
        this.orderParam.leaveTime = this.$moment(
          this.orderParam.leaveTime
        ).format("YYYY-MM-DD");
      }
      this.$axios.post("api/employeeLeave/queryLeave", pageParam).then(res => {
        if (res.data.code == 200) {
          let pageData = res.data.data;
          this.tableData = pageData.list;
          //给分页参数赋值
          this.total = pageData.total;
          this.pageNum = pageData.pageNum;
          this.pageSize = pageData.pageSize;
        }
      });
    },
    addEmployeeLeave() {
      if (this.employee.leaveTime) {
        this.employee.leaveTime = this.$moment(this.employee.leaveTime).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      } else {
        this.employee.leaveTime = "";
      }
      let employLeaveDTO = { employee: this.employee, empid: this.operator.id };
      this.$axios
        .post("api/employeeLeave/addEmployeeLeave", employLeaveDTO)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success("添加成功!");
            this.queryEmployee();
            this.putawayVisible = false;
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
    //查所有未离职的员工
    queryNowEmp() {
      this.$axios.get("api/employee/queryNowEmp").then(res => {
        if (res.data.code == 200) {
          this.employeeList = res.data.data;
        }
      });
    },
    //切换员工名字时候触发
    handleEmployeeSelect(value) {
      const selectedEmployee = this.employeeList.find(
        item => item.name === value
      );
      if (selectedEmployee) {
        this.employee = JSON.parse(JSON.stringify(selectedEmployee));
        // 这里增加深拷贝使得 this.employee 和 this.employeeList 中的对应项实际上是两个完全独立的对象
      }
    },
    //查询当前操作人
    queryOperator() {
      this.$axios.get("api/employee/queryOperator").then(res => {
        if (res.data.code == 200) {
          this.operator = res.data.data;
        }
      });
    },
    //查看展示员工信息
    openLookDialog(val) {
      this.$axios
        .get("api/employeeLeave/queryLeaveEmployee/" + val)
        .then(res => {
          if (res.data.code == 200) {
            this.employee1 = res.data.data;
            //根据操作人id查操作人
            this.$axios
              .get("api/employee/queryById/" + this.employee1.leaveOperator)
              .then(res => {
                if (res.data.code == 200) {
                  this.leaveOperatorName = res.data.data.name;
                }
              });
          }
        });
      this.lookVisible = true; // 打开弹窗
    }
  },
  created() {
    this.queryEmployee();
    this.queryNowEmp();
    this.queryOperator();
  },
  computed: {
    genderText() {
      return this.employee.gender == 0 ? "女" : "男";
    },
    genderText1() {
      return this.employee1.gender == 0 ? "女" : "男";
    },
    leaveTypeText1() {
      const leaveTypes = ["自动离职", "解雇", "辞职", "其他"];
      return leaveTypes[this.employee1.leaveType];
    }
  }
};
</script>
    
<style scoped>
.el-table {
  margin-top: 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
.el-form-item {
  margin: 10px 50px;
}
.el-button {
  border-radius: 20px;
  text-align: center; /* 文字居中  */
  border: none;
  color: white; /* 文字颜色白色  */
  cursor: pointer;
  display: inline-block;
  background: linear-gradient(
    to right,
    rgb(176, 119, 218),
    rgb(129, 214, 238)
  ); /* 渐变效果更换为浅紫色到浅蓝色 */
  transition-property: background-color, transform; /* 添加过渡属性 */
  transition-duration: 0.3s; /* 过渡持续时间 */
}

.el-button:hover {
  transform: scale(1.1); /* 按钮放大效果  */
  transition: transform .3s ease-in-out;
}

.el-pagination {
  margin-top: 20px; /* 或者你觉得合适的值 */
}
.col-in-page {
  position: relative;
  margin: 1rem;
  padding: 2.5rem;
  background: white;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  overflow: hidden;
  background: linear-gradient(60deg, #c4dcda 0%, #96e4e1 100%);
}
.el-pagination {
  margin-top: 20px; /* 或者你觉得合适的值 */
}
.form-required:before {
  content: "*";
  color: red;
}
</style>